package com.wei.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wei.entity.GdProperty;
import com.wei.mapper.GdPropertyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李泽伟
 * @since 2022-03-08
 */
@RestController
@RequestMapping
public class GdPropertyController {
    @Autowired
    private GdPropertyMapper propertyMapper;

    @RequestMapping("/getProperty")
    public List<GdProperty> getProperty(){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        List<GdProperty> properties=propertyMapper.selectByMap(map);
        return properties;
    }

    @RequestMapping("/addProperty")
    public void setProperty(@RequestParam Map<String,Object> propertyObj){
        GdProperty property =setParam(propertyObj);
        propertyMapper.insert(property);
    }

    @RequestMapping("/updateProperty")
    public void updateProperty(@RequestParam Map<String,Object> propertyObj){
        GdProperty property = setParam(propertyObj);
        propertyMapper.updateById(property);
    }

    @RequestMapping("/deleteProperty")
    public void deleteProperty(@RequestParam String propertyId){
        propertyMapper.deleteById(propertyId);
    }

    /**
     * 判断物业地址是否存在
     * @param propertyAddress
     * @return
     */
    @RequestMapping("/hasPropertyAddress")
    public Boolean hasPropertyAddress(@RequestParam String propertyAddress){
        QueryWrapper<GdProperty> wrapper=new QueryWrapper<>();
        wrapper.eq("property_address",propertyAddress);
        wrapper.eq("deleted",0);
        if(propertyMapper.selectOne(wrapper)==null){
            return false;
        } else {
            return true;
        }
    }

    /**
     * 判断两个物业地址是否存在
     * @param propertyAddress
     * @return
     */
    @RequestMapping("/hasPropertyAddresss")
    public int hasPropertyAddresss(@RequestParam Map<String,Object> propertyAddress){
        int hasAddress1=-1;
        int hasAddress2=-1;
        QueryWrapper<GdProperty> wrapper1=new QueryWrapper<>();
        wrapper1.eq("property_address",propertyAddress.get("propertyAddress1"));
        wrapper1.eq("deleted",0);
        if(propertyMapper.selectOne(wrapper1)==null){
            hasAddress1=0;
        } else {
            hasAddress1=1;
        }
        QueryWrapper<GdProperty> wrapper2=new QueryWrapper<>();
        wrapper2.eq("property_address",propertyAddress.get("propertyAddress2"));
        wrapper2.eq("deleted",0);
        GdProperty property=propertyMapper.selectOne(wrapper2);
        if(property==null){
            hasAddress2=0;
        } else if(property.getPropertyIssell().equals("否")){
            hasAddress2=1;
        }else {
            hasAddress2=2;
        }
        System.out.println(hasAddress1);
        System.out.println(hasAddress2);
        if(hasAddress1 ==0 && hasAddress2==0){
            return 0;   //两个地址都不存在
        }else if(hasAddress1 ==0 && hasAddress2==1){
            return 1;   //第一个地址不存在,第二个地址未出售
        }else if(hasAddress1 ==0 && hasAddress2==2){
            return 5;   //第一个地址不存在
        } else if(hasAddress1 ==1 && hasAddress2==0){
            return 2;   //第二个地址不存在
        }else if(hasAddress1 ==1 && hasAddress2==1){
            return 3;   //第二个地址未售出
        }else {
            return 4;   //两个地址都存在
        }
    }

    /**
     * 通过物业地址获取房产信息，用户房产信息的展示(用户)
     * @param address
     * @return
     */
    @RequestMapping("/getPropertyByAddress")
    public GdProperty getPropertyByAddress(@RequestParam String address){
        QueryWrapper<GdProperty> wrapper=new QueryWrapper<>();
        wrapper.eq("property_address",address);
        wrapper.eq("deleted",0);
        GdProperty property=propertyMapper.selectOne(wrapper);
        return property;
    }

    /**
     * 设置参数方法
     * @param propertyObj
     * @return
     */
    private GdProperty setParam(@RequestParam Map<String, Object> propertyObj) {
        GdProperty property=new GdProperty();
        property.setPropertyId((String) propertyObj.get("propertyId"));
        property.setPropertyForeignkey((String) propertyObj.get("propertyForeignkey"));
        property.setPropertyAddress((String) propertyObj.get("propertyAddress"));
        property.setPropertyIssell((String) propertyObj.get("propertyIssell"));
        property.setHouseholdName((String) propertyObj.get("householdName"));
        property.setPropertyType((String) propertyObj.get("propertyType"));
        property.setPropertyDecorate((String) propertyObj.get("propertyDecorate"));
        property.setPropertyAllarea(Integer.parseInt(propertyObj.get("propertyAllarea").toString()));
        property.setPropertyUsearea(Integer.parseInt(propertyObj.get("propertyUsearea").toString()));
        property.setPropertyUsername((String) propertyObj.get("propertyUsername"));
        property.setPropertyPwd((String) propertyObj.get("propertyPwd"));
        return property;
    }

}

